// 购物车
$('.shopping_cart').on({
    'click': function () {
        let isLogin = localStorage.getItem('isLogin');
        let bool = isLogin == 'true';
        if (bool) {
            location.href = './jd-shopping_Cart.html';
        } else {
            location.href = './index.html';
        }
    },
    'mouseover': function () {
        let isLogin = localStorage.getItem('isLogin');
        let bool = isLogin == 'true';
        if (bool) {

            let str = `<h4>最新加入的商品</h4>`;
            orderList.forEach(item => {
                str += `
                <section class="order-goods">
                <ul>
                    <li>${item.discount}</li>
                    <li>
                        <img src="${item.img}" alt="">
                    </li>
                    <li>
                        <h6>${item.name}</h6>
                        <p>${item.details}</p>
                    </li>
                    <li>
                        <!-- 价格x数量 -->
                        <span style="font-weight: 700;">${item.price}x${item.num}</span>
                        <span>删除</span>
                    </li>
                </ul>
            </section>`
            });
            $(this).find('.dropdown-layer').html(str)
            $('.order-goods').hover(function () {
                $(this).css('backgroundColor', '#f5f5f5')
            })
        } else {
            let str2 = ``;
            str2 = `  <section class="myjdlist">
                        <span></span>
                    </section>`
            $(this).find('.dropdown-layer').html(str2)
        }
    }
})


// 更多服务-模块
function updateService() {
    let str = ``;
    services.forEach((obj) => {
        str += `<li>
                <img src="${obj.img}" alt="">
                <div>${obj.title}</div>
              </li>`
    })
    $('.other ul').html(str);
}
updateService();
$('.other li').hover(function () {
    let index = $(this).index();
    $(this).find('img').attr('src', services[index].hover)
}, function () {
    let index = $(this).index();
    $(this).find('img').attr('src', services[index].img)
})

// 秒杀倒计时
function countDown() {
    var nowHour = new Date().getHours();
    var nowMinute = new Date().getMinutes();
    var nowSecond = new Date().getSeconds();
    var targetHour = 0;
    // 点数为2的倍数
    if (nowHour % 2 == 0) {
        target = 7200 - nowMinute * 60 - nowSecond;
        targetHour = nowHour;
    } else {
        //点数不是2的倍数想·
        target = 3600 - nowMinute * 60 - nowSecond;
        targetHour = nowHour - 1;
    }
    targetHour = targetHour >= 10 ? targetHour : '0' + targetHour;
    let min = parseInt(target / 60 % 60);
    let sec = parseInt(target % 60);
    min = min >= 10 ? min : '0' + min;
    sec = sec >= 10 ? sec : '0' + sec;
    $('.countdown-desc strong').html(targetHour + ':00')
    $('.hour').html('0' + parseInt(target / 60 / 60));
    $('.minute').html(min);
    $('.second').html(sec);
}
countDown();
setInterval(countDown, 1000);
var picStates = [-160, 0];

function picChange() {
    $('.seckill-content').each((i, item) => {
        $(item).css({
            left: picStates[i]
        })
    })
}
picChange()
// 每日特价
var dailyIndex = 0
function updateDaily() {
    // 大图
    let dailyStr = `<img src="${everyday[dailyIndex][0].img}[" alt="">
    <p>${everyday[dailyIndex][0].name}</p>
    <b>${everyday[dailyIndex][0].price}</b>
    <div class="lowest">${everyday[dailyIndex][0].lowest}</div>
    `;
    $('.daily-show-left').html(dailyStr);
    // 小图
    let dailyStr2 = ``;
    everyday[dailyIndex].forEach((obj, i) => {
        if (i != 0) {
            dailyStr2 += `<li>
            <div class="daily-show-img">
                <img src="${obj.img}" alt="">
                <div class="lowest">${obj.lowest}</div>
            </div>
            <div class="daily-show-text">
                <p>${obj.name}</p>
                <b>${obj.price}</b>
                <span>已抢<span>${obj.soldNum}</span>件</span>
            </div>
        </li>`
        }
    })
    $('.daily-show-right').html(dailyStr2);
    $('.daily-show-text>span').each((i, val) => {
        if ($(val).text() == '已抢undefined件') {
            $(val).remove()
        }
    })
    $('.lowest').each((j, val2) => {
        if ($(val2).text() == 'undefined') {
            $(val2).remove()
        }
    })
}
updateDaily();
$('.daily-title-right span').mouseover(function () {
    $(this).addClass('daily-title-right-Aciive');
    $(this).siblings().removeClass('daily-title-right-Aciive');
    dailyIndex = $(this).index()
    updateDaily();
})
// 

// 频道广场
for (let i = 0; i < channelData.length; i++) {
    let channelStr = `<a href="">
            <span>${channelData[i].type}</span>
            <span>${channelData[i].title}</span></a>
            <img src="${channelData[i].img1}" alt="">
            <img src="${channelData[i].img2}" alt="">`;
    $('.channels_item-1').eq(i).html(channelStr);
}
// JOY寻宝
// 标题渲染
var tre_title_Str = ``;
treasureData.forEach((obj, i) => {
    tre_title_Str += `<span class=${i==0?'joy-title-Active':''}>${obj.title}</span>`
})
$('.joy-title').html(tre_title_Str);
// 内容渲染
var treIndex = 0;
function update_joy_content() {
    var tre_content_Str = ``;
    treasureData[treIndex].goods.forEach((item) => {
        tre_content_Str += `<section>
        <img src="${item.img}" alt="">
        <div class="joy-content-txt">
            <p>${item.name}</p>
            <span class="joy_content_price"><i>¥</i>${item.price}</span>
        </div>
    </section>`
    })
    $('.joy-content').html(tre_content_Str)
}
update_joy_content()
$('.joy-title span').mouseover(function () {
    $(this).addClass('joy-title-Active').siblings().removeClass('joy-title-Active');
    treIndex = $(this).index();
    update_joy_content();
})
// 为你推荐
// 标题渲染
var titleStr = ``;
modules.forEach((obj, i) => {
    titleStr += ` <li class=${i==0?'recommend-Active':''}>
    <button class="recommend-btn">${obj.title}</button>
    <p>${obj.desc}</p></li>`;
})
$('.recommend-title ul').html(titleStr)
// 默认渲染第一模块
var moduleIndex = 0;
function updateModule() {
    let moduleStr = ``;
    modules[moduleIndex].goods.forEach((obj, i) => {
        moduleStr += `<li>
        <div class="recommend-goods-pic">
            <img src="${obj.goodsList[0].img}" alt="">
        </div>
        <p class="more2_info_name">
            <i class="more2_info_self">${obj.goodsList[0].saleMol}</i>${obj.goodsList[0].name}</p>
        <span class="mod_price"><i>¥</i>
        ${obj.goodsList[0].price}</span>
        <span class="more2_discount">${obj.goodsList[0].saleAct}</span>
    </li>`
    })
    $('.recommend-goods ul').html(moduleStr);
    $('.more2_info_self').each((i, val) => {
        if ($(val).text() == '') {
            $(val).remove()
        }
    })
    $('.more2_discount').each((j, val) => {
        if ($(val).text() == '') {
            $(val).remove()
        }
    })
}
updateModule();
$('.recommend-title li').click(function () {
    // console.log($(this).index());
    moduleIndex = $(this).index();
    $(this).addClass('recommend-Active').siblings().removeClass('recommend-Active');
    updateModule();
    $(document).scrollTop(2860)
})
// $('.recommend-title li').hover(function(){
//     if($(this).index()!=moduleIndex){
//         $(this).find('button').css('color','#c81623');
//         $(this).find('p').css('color','#c81623');
//     }
// },function(){
//     if($(this).index()!=moduleIndex){
//         $(this).find('button').css('color','#000');
//         $(this).find('p').css('color','#999');
//     }
// })
// 滚动监听
$(document).scroll(function () {
    console.log($(document).scrollTop());
    if ($(document).scrollTop() >= 180) {
        // 二级菜单固定
        $('.secondary-menu').addClass('scroll-menu');
    } else {
        $('.secondary-menu').removeClass('scroll-menu')
    }
    if ($(document).scrollTop() >= 480) {
        // 搜索框固定
        $('header').addClass('input-appearent');
        // 侧栏固定
        $('.spike-activity').css('position', 'static')
        $('.elevator_list').addClass('elevator_list-fixed');
    } else {
        $('header').removeClass('input-appearent');
        $('.spike-activity').css('position', 'relative')
        $('.elevator_list').removeClass('elevator_list-fixed');
    }
    if ($(document).scrollTop() >= 2960) {
        // 为你推荐导航栏固定
        $('.recommend-title').addClass('recommend-title-fixed');
        $('.elevator_list-fixed').css('top','130px')
    } else {
        $('.recommend-title').removeClass('recommend-title-fixed');
        $('.elevator_list-fixed').css('top','80px')
    }
})
// 侧栏点击跳转到指定的模块
var redirectArr=[{index:0,top:488},{index:1,top:1395},{index:2,top:1755},{index:3,top:2780},{},{},{index:6,top:0}]
$('.spike-activity>ul li').click(function () {
    if($(this).index()==redirectArr[$(this).index()].index){
        $(document).scrollTop(redirectArr[$(this).index()].top)
    }
})
 // 跳转到搜索商品页面
 function search(){
    var search_goods=[]
    let keyword = $('.form input').val()
    modules.forEach((obj) => {
        obj.goods.forEach((every) => {
            if (every.goodsList[0].name.includes(keyword)) {
                search_goods.push(every.goodsList[0])
            }
        })
    })
    let str=JSON.stringify(search_goods)
    localStorage.setItem('search_goods',str)
   
 }
 $('.form input')[0].onkeyup=function(e){
    if(e.keyCode==13){
        search()
        // $('.form input').val('')
        location.href='./jd-search.html';
    }
}
$('.icon-fangdajing').click(function(){
    search()
    location.href='./jd-search.html';
})
